package com.wisdom.benefit.jufa.main;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.bean.HomeListBean;
import com.wisdom.benefit.jufa.event.CallBackInterface;

import java.util.ArrayList;

public class HomeAdapter extends BaseAdapter {
    private ArrayList<HomeListBean> datas;
    private Context context;
    private CallBackInterface callback; //回调接口

    public HomeAdapter(ArrayList<HomeListBean> datas, Context context, CallBackInterface callback) {
        this.datas = datas;
        this.context = context;
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_item, parent, false);
            holder = new ViewHolder();
            holder.nameTv = (TextView) convertView.findViewById(R.id.home_name_tv);
            holder.addressTv = (TextView) convertView.findViewById(R.id.home_address_tv);
            holder.accountTv = (TextView) convertView.findViewById(R.id.home_account_tv);
            holder.statusTv = (TextView) convertView.findViewById(R.id.home_status_tv);
            holder.closeBtn = convertView.findViewById(R.id.home_do_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (datas.get(position).getPassagewayType()==1) {
            holder.nameTv.setText("支付宝固码");
        } else if (datas.get(position).getPassagewayType()==2) {
            holder.nameTv.setText("微信固码");
        }else if (datas.get(position).getPassagewayType()==4) {
            holder.nameTv.setText("支付宝吱口令");
        } else {
            holder.nameTv.setText("支付宝转账");
        }
        if (datas.get(position).getStatus() == 1){
            holder.statusTv.setText("开启");
            holder.closeBtn.setText("关闭");
            if (TextUtils.isEmpty(datas.get(position).getPaymentCodePayeeName())){
                holder.accountTv.setText("无收款码");
            }else{
                holder.accountTv.setText(datas.get(position).getPaymentCodePayeeName());
            }
        }else{
            holder.statusTv.setText("关闭");
            holder.closeBtn.setText("开启");
            holder.accountTv.setText("非收款账户");
            }

        holder.addressTv.setText(datas.get(position).getArea());

        holder.closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.callBackClick(position);
            }
        });
        holder.closeBtn.setTag(position);
        return convertView;
    }

    static class ViewHolder {
        TextView nameTv, addressTv;
        TextView accountTv, statusTv;
        Button closeBtn;
    }
}
